<?php

namespace App\Http\Resources\Game\Other\Broadcast;

use App\Http\Resources\Resource;

class DataResource extends Resource
{

    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $data = parent::toArray($request);

        $data = empty($data) ? [] : $this->makeData($data);

        return $this->getResponse()->setResponseData($data);
    }

    /**
     * 制作数据
     *
     * @param $data
     * @return array
     */
    protected function makeData($data)
    {
        foreach ($data['contents'] as &$content) {
            $content['content_format'] = sprintf($content['content'], ...$data['content_param']);
        }
        $data['content_ids'] = array_column($data['contents'], 'id');

        return $data;
    }
}
